<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *订单管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$tabs=array(
	array('op'=>'list','title'=>'全部记录'),
	array('op'=>'add','title'=>'手动入单'),
); 
if($_RQ['op']=='list'){
	$return['params']=array();
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if(isset($_RQ['status']) && $_RQ['status']!=100){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=intval($_RQ['status']);
	}
	if($_RQ['mid']){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=intval($_RQ['mid']);
	}
	$pindex = max(1, intval($_RQ['page']));
	if($_RQ['psize']){
		$_SESSION['psize']=$_RQ['psize'];
	}
	$psize = $_SESSION['psize']?:10;
	$list=pdo_getslice('jy_weishopwater_yalog',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as $k=>&$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			$item['gtitle']=pdo_getcolumn('jy_weishop_goods_group',array('id'=>$item['gid']),'title');
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','total'=>$total,'psize'=>$psize,'edit'=>false,'del_one'=>true,'delall'=>false,'list'=>array(
			array('name'=>'member','title'=>'会员信息_Member','type'=>'member'),
			array('name'=>'gtitle','title'=>'桶分类','type'=>'string'),
			array('name'=>'fee','title'=>'押金','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),
			array('name'=>'createtime','title'=>'时间','type'=>'time'),
		),'action'=>array(
			array('op'=>'pay','title'=>'确认退款','type'=>'modal','show'=>array('status'=>array(3))),
		)),
		'search'=>array(
			array('name'=>'status','width'=>15,'title'=>'状态','type'=>'radio','options'=>'status'),
			array('type'=>'choose','width'=>15,'action'=>'core/member.get_member/','name'=>'mid','title'=>'选择会员'),
		),
	);
	$return['status']=$this->yalog_status;
	$return['htmls']=$htmls;	
	exi($return);
}
if($_RQ['op']=='add'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$groupid = $post['groupid'];
		if(!$groupid){
			exi('请选择水票分类押金！','error');
		}
		if(!$post['mid']){
			exi('请选择会员！','error');
		}
		$yagoods=pdo_get('jy_weishopwater_goods',array('uniacid'=>$_SESSION['uniacid'],'groupid'=>$groupid));
		$yalog=pdo_get('jy_weishopwater_yalog',array('uniacid'=>$_SESSION['uniacid'],'gid'=>$groupid,'mid'=>$post['mid']));
		if($yalog['status']){
			//exi('该用户，水桶已交押金！','error');
		}
		$this->model('model')->addlog($yagoods,$post['mid']);
		exi('手动录单成功！','','list');
	}
	$item=array();
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'desc'=>'手动录单',
		'tabs'=>$tabs,
		'edit'=>array(
			array('title'=>'老客户/客户给现金，手动录单！','type'=>'tip'),
			array('type'=>'choose','title'=>'选择会员','name'=>'mid','action'=>'core/member.get_member/','choose_title'=>'选择会员'),
			array('name'=>'groupid','title'=>'选择水桶分类押金','type'=>'select','options'=>'groups'),
		)
	);
	$return['groups']=pdo_getall('jy_weishop_goods_group',array('onepluginname'=>'jy_weishopwater','uniacid'=>$_SESSION['uniacid'],'parentid'=>0,'mid'=>0));
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='pay'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		$this->model('model')->refund_pay($id);
		exi('打款成功！','','list');
	}
	$id=intval($_RQ['id']);
	$item=pdo_get('jy_weishopwater_yalog',array('id'=>$id));
	if(!$item){
		exi('指定退款申请不存在~','error');
	}	
	$member=model('core/core')->member_show($item['mid']);
	$item['wxgid']=pdo_getcolumn('core_fans',array('mid'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']),'wxgid');
	if($member['from_os']!=2){
		$item['apptitle']=pdo_getcolumn('core_account_wxapp',array('id'=>$item['wxgid']),'title');
	}else{
		$item['apptitle']=pdo_getcolumn('core_account_wechat',array('id'=>$item['wxgid']),'title');
	}
	$item['apptitle']=$item['apptitle']?:'无';
	$item['paytype']=pdo_getcolumn('core_paylog',array('id'=>$item['pay_id'],'uniacid'=>$_SESSION['uniacid']),'pay_type')?:'credit';
	$return['paytype']=array('credit'=>'退款到余额','wechat'=>'退款到微信','alipay'=>'退款到支付宝');	
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'edit',
		'edit'=>array(
			array('type'=>'tip','title'=>'【温馨提示】：如果打款会员是<red>小程序</red>会员，会显示所属《小程序》，否则可能会打款不成功!默认打款使用的是公众号！'),
			array('name'=>'apptitle','title'=>'所属','type'=>'copy'),
			array('type'=>'radio','name'=>'paytype','title'=>'退押金方式','options'=>'paytype'),
			array('type'=>'copy','name'=>'fee','title'=>'退押金金额'),
		),
	);
	exi($return);
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_weishopwater_yalog',array('id'=>$id));	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_weishopwater_yalog',array('id'=>$id));
			}
		}
	}
	exi('删除成功！');
}
